//
//  SNPleaseLogInAlertView.m
//  app
//
//  Created by 王少宁 on 15/4/23.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//  请重新登陆的 alerView

#import "SNPleaseLogInAlertView.h"
#import "HDAppDelegate.h"
#import "HDLoginoutViewController.h"
#import "SNSessionManager.h"
#import "ParticipationDataManager.h"
#import "HZMallSqlDataManager.h"

@implementation SNPleaseLogInAlertView

+(void)pleaseLogIn
{
    HDAppDelegate* app = (HDAppDelegate*)[[UIApplication sharedApplication]delegate];
    
    SNPleaseLogInAlertView * pleaseLoginAlertView = [[SNPleaseLogInAlertView alloc] initWithTitle:@"提示" message:@"用户未登录或者已离线,请重新登录..." delegate:app cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:pleaseLoginAlertView];
    [pleaseLoginAlertView show];
    
    //清除 cookies
    [SNSessionManager clearCookies];
    
    /** 删除本地缓存信息 -write by khzliu */
    [[HZMallSqlDataManager defaultManager]  deleteAllProductChoseStatus];
    [[HZMallSqlDataManager defaultManager] deleteAllAddressCacheData];
    [[HZMallSqlDataManager defaultManager] deleteAllSearchHistoryCacheData];
    [[HZMallSqlDataManager defaultManager] deleteAllOfflineCartsCacheData];
}

#pragma mark - 点击了确定按钮
- (void)afterLoginBtnClicked
{
    NSString * uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"];
    NSString * allInformation = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults]removePersistentDomainForName:allInformation];

    [[SDImageCache sharedImageCache]removeImageForKey:@"userHead"];
    [[SDImageCache sharedImageCache]removeImageForKey:@"userLevel_icon"];
    [[SDImageCache sharedImageCache]removeImageForKey:@"userNlv_icon"];
    
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLaunch"];
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"userLogout"];
    [[NSUserDefaults standardUserDefaults] setObject:uid forKey:@"mark"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    HDAppDelegate* app = (HDAppDelegate*)[[UIApplication sharedApplication]delegate];
    app.storyBoard = [UIStoryboard storyboardWithName:@"LogInOutStoryboard" bundle:nil];
    
    [UIView transitionWithView:app.window duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^(void) {
        BOOL oldState = [UIView areAnimationsEnabled];
        
        [UIView setAnimationsEnabled:NO];
        
        NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:@"YES" forKey:@"userLogout"];
        
        app.window.rootViewController = [app.storyBoard instantiateInitialViewController];
        [self.window makeKeyAndVisible];
        [UIView setAnimationsEnabled:oldState];
    } completion:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
